// pages/mockExam/mockExam.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    paperId: "",
    paperName: "",
    questionList: [],
    totalNum: "",
    selectIndex: 0,
    showMask: false,
    isSubmit: false,
    showPage: false,
    answerNum: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData(options)
    wx.setNavigationBarTitle({
      title: options.paperName
    })
    this.getPaperInfo()
  },
  getPaperInfo() {
    let obj = {
      page: 0,
      size: 100
    }
    app.api.getPaperInfo(this.data.paperId, obj)
      .then(res => {
        wx.hideLoading()
        let questionList = res.content
        let arr = []
        questionList.forEach(item => {
          let obj = {}
          let titleObj = JSON.parse(item.question.body)
          obj.typeText = this.formatType(item.question.type)
          obj.title = titleObj.title
          obj.imgSrc = titleObj.imgSrc 
          obj.audioSrc = titleObj.audioSrc
          obj.videoSrc = titleObj.videoSrc
          obj.type = item.question.type
          obj.trueVal = item.question.answer
          obj.answerId = item.id
          obj.videoSrc ? obj.showMask = true : ""
          if (item.question.options.length) {
            let arr1 = []
            item.question.options.forEach((item1, index) => {
              arr1.push({
                text: this.formatOpt(index + 1) + item1,
              })
            })
            obj.options = arr1
          }
          arr.push(obj)
        })
        this.setData({
          totalNum: arr.length,
          questionList: arr,
          showPage: true
        })
        this.initVideo()
      })
  },
  initVideo(){
    let questionList = this.data.questionList
    questionList.forEach((item,index) => {
      if (item.videoSrc) this['videoContext' + index] = wx.createVideoContext('video' + index)
    })
  },
  bindPlay(e){
    let index = e.currentTarget.dataset.index
    // console.log(this)
    this.setData({
      ['questionList[' + index + '].showMask']: false
    })
    this['videoContext' + index].requestFullScreen()
    this['videoContext' + index].play()
  },
  bindfullscreenchange(e){
    console.log(e)
    let index = e.currentTarget.dataset.index
    if (!e.detail.fullScreen) {
      this.setData({
        ['questionList[' + index + '].showMask']: true
      })
    }
  },
  //打开选项板
  openMask() {
    this.setData({
      showMask: !this.data.showMask
    })
  },
  //关闭选项版
  closeMask() {
    this.setData({
      showMask: false
    })
  },
  preShow() {
    if (this.data.selectIndex == 0) return
    this.setData({
      selectIndex: this.data.selectIndex - 1,
      showMask: false
    })
  },
  nextShow() {
    if (this.data.selectIndex + 1 == this.data.totalNum) return
    this.setData({
      selectIndex: this.data.selectIndex + 1,
      showMask: false
    })
  },
  changeIndex(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      selectIndex: index,
      showMask: false
    })
  },
  submit(){
    if (this.data.isSubmit) return
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '您确定要交卷吗？交卷之后错题自动显示',
      success: function (res) {
        if (res.confirm) {
          let questionList = _this.data.questionList
          let errorNum = 0
          questionList.forEach(item => {
            if (item.check == 'false'){
              errorNum++
              item.trueText = _this.formatVal(String(item.trueVal))
              item.falseText = _this.formatVal(String(item.answer))
            }
          })
          wx.showToast({
            title: '你有' + errorNum + '道错题，快去查看吧！',
            icon: 'none'
          })
          _this.setData({
            isSubmit: true,
            showMask: true,
            questionList: questionList
          })
        } else if (res.cancel) {
          // _this.setData()
        }
      }
    })
  },
  formatVal(val) {
    return val.replace(/0|1|2|3|4|5|6|false|true/g, function (val) {
      switch (val) {
        case "0":
          return "A";
          break;
        case "1":
          return "B";
          break;
        case "2":
          return "C";
          break;
        case "3":
          return "D";
          break;
        case "4":
          return "E";
          break;
        case "5":
          return "F";
          break;
        case "6":
          return "G";
          break;
        case "false":
          return "错误";
          break;
        case "true":
          return "正确";
          break;
      }
    })
  },
  formatOpt(index) {
    switch (index) {
      case 1:
        return "A "
        break;
      case 2:
        return "B "
        break;
      case 3:
        return "C "
        break;
      case 4:
        return "D "
        break;
      case 5:
        return "E "
        break;
      case 6:
        return "F "
        break;
      case 7:
        return "G "
        break;
    }
  },
  formatType(type) {
    switch (type) {
      case 1:
        return "单选题"
        break;
      case 2:
        return "多选题"
        break;
      case 3:
        return "判断题"
        break;
    }
  },
  currentChange(e) {
    this.setData({
      selectIndex: e.detail.current
    })
  },
  //判断题选择
  checkOpt(e) {
    // console.log(this.data.selectIndex)
    if(this.data.isSubmit) return
    let val = e.currentTarget.dataset.val
    this.setData({
      ["questionList[" + this.data.selectIndex + "].answer"]: String(val)
    })
    this.updateAnswer()
  },
  // 单选题选择
  radioOpt(e) {
    if (this.data.isSubmit) return
    // console.log(e)
    let index = e.currentTarget.dataset.index
    this.setData({
      ["questionList[" + this.data.selectIndex + "].answer"]: String(index)
    })
    this.updateAnswer()
  },
  //多选题选择
  selectOpt(e) {
    if (this.data.isSubmit) return
    // console.log(e)
    let index = e.currentTarget.dataset.index
    let val = !this.data.questionList[this.data.selectIndex].options[index].checked
    this.setData({
      ["questionList[" + this.data.selectIndex + "].options[" + index + "].checked"]: val
    })
    let arr = []
    this.data.questionList[this.data.selectIndex].options.forEach((item, index) => {
      if (item.checked) {
        arr.push(index)
      }
    })
    this.setData({
      ["questionList[" + this.data.selectIndex + "].answer"]: arr.length ? "[" + arr.join(",") + "]" : ""
    })
    this.updateAnswer()
  },
  //用户选择答案更新
  updateAnswer() {
    let index = this.data.selectIndex
    let currentObj = this.data.questionList[index]
    let answerId = currentObj.answerId
    let score = 0
    let answerNum = 0
    this.data.questionList.forEach(item => {
      if (item.answer) answerNum++
    })
    if (currentObj.answer == currentObj.trueVal) {
      score = 3
    }
    this.setData({
      ["questionList["+index+"].check"]: score == 0 ? "false" : "true",
      answerNum: answerNum
    })
    let obj = {
      score: score,
      answer: currentObj.answer
    }
   // app.api.updateAnswer(answerId, obj)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})